home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 March: Reference Library / Dev.CD Mar 97 RL.toast / mac / Technical Documentation / develop / develop Issue 24 / develop Issue 24 code / Scriptable Database 1.0a15.sea / Scriptable Database 1.0a15 / Database / Transaction.cp / Transaction.cp
Encoding:
Text File  |  1996-04-29  |  7.0 KB  |  189 lines  |  [TEXT/CWIE]

  1. //================================================================================
  2. // Greg Anderson
  3. // db+
  4. //
  5. // Transaction object
  6. // 17 May 1994
  7. // 31 Dec 1994
  8. //================================================================================
  9.  
  10. #include "Transaction.h"
  11.  
  12. #include "DBElement.h"
  13. #include "DBProperty.h"
  14. #include "DataRecord.h"
  15.  
  16. //--------------------------------------------------------------------------------
  17. // TTransaction::~TTransaction
  18. //--------------------------------------------------------------------------------
  19. TTransaction::~TTransaction()
  20. {
  21.     this->DiscardChanges();
  22. } // TTransaction::~TTransaction
  23.  
  24. //--------------------------------------------------------------------------------
  25. // TTransaction::GetUpdatePointer
  26. //--------------------------------------------------------------------------------
  27. TTransactionAwareObject* TTransaction::GetUpdatePointer(AConst<TTransactionAwareObject> object)
  28. {
  29.     Require(object.Exists());
  30.     TTransactionAwareObject* newUP = nil;
  31.  
  32.     newUP = object->GrabUpdatePointer(this);
  33.  
  34.     REQUIREVALIDPOINTER(newUP);
  35.     
  36.     return newUP;
  37. } // TTransaction::GetUpdatePointer
  38.  
  39. //--------------------------------------------------------------------------------
  40. // TTransaction::GetDBRecordUpdatePointer
  41. //--------------------------------------------------------------------------------
  42. AnUpdate<TDBRecord> TTransaction::GetDBRecordUpdatePointer(AConst<TTransactionAwareObject> cursor)
  43. {
  44.     TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
  45.     return newUP->AbstractDBRecord();
  46. } // TTransaction::GetDBRecordUpdatePointer
  47.  
  48. //--------------------------------------------------------------------------------
  49. // TTransaction::GetDBElementUpdatePointer
  50. //--------------------------------------------------------------------------------
  51. AnUpdate<TDBElement> TTransaction::GetDBElementUpdatePointer(AConst<TTransactionAwareObject> cursor)
  52. {
  53.     TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
  54.     return newUP->DBElementRecord();
  55. } // TTransaction::GetDBElementUpdatePointer
  56.  
  57. //--------------------------------------------------------------------------------
  58. // TTransaction::GetDBPropertyUpdatePointer
  59. //--------------------------------------------------------------------------------
  60. AnUpdate<TDBProperty> TTransaction::GetDBPropertyUpdatePointer(AConst<TTransactionAwareObject> cursor)
  61. {
  62.     TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
  63.     return newUP->DBPropertyRecord();
  64. } // TTransaction::GetDBPropertyUpdatePointer
  65.  
  66. //--------------------------------------------------------------------------------
  67. // TTransaction::GetDataRecordUpdatePointer
  68. //--------------------------------------------------------------------------------
  69. AnUpdate<TDataRecord> TTransaction::GetDataRecordUpdatePointer(AConst<TTransactionAwareObject> cursor)
  70. {
  71.     TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
  72.     return newUP->DataRecord();
  73. } // TTransaction::GetDataRecordUpdatePointer
  74.  
  75. //--------------------------------------------------------------------------------
  76. // TTransaction::GetDBElementUpdatePointer
  77. //--------------------------------------------------------------------------------
  78. AnUpdate<TDBRecord> TTransaction::GetDBRecordUpdatePointer(AConst<TDBRecord> cursor)
  79. {
  80.     TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
  81.     return newUP->AbstractDBRecord();
  82. } // TTransaction::GetDBElementUpdatePointer
  83.  
  84. //--------------------------------------------------------------------------------
  85. // TTransaction::GetDBElementUpdatePointer
  86. //--------------------------------------------------------------------------------
  87. AnUpdate<TDBElement> TTransaction::GetDBElementUpdatePointer(AConst<TDBElement> cursor)
  88. {
  89.     TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
  90.     return newUP->DBElementRecord();
  91. } // TTransaction::GetDBElementUpdatePointer
  92.  
  93. //--------------------------------------------------------------------------------
  94. // TTransaction::GetDBPropertyUpdatePointer
  95. //--------------------------------------------------------------------------------
  96. AnUpdate<TDBProperty> TTransaction::GetDBPropertyUpdatePointer(AConst<TDBProperty> cursor)
  97. {
  98.     TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
  99.     return newUP->DBPropertyRecord();
  100. } // TTransaction::GetDBPropertyUpdatePointer
  101.  
  102. //--------------------------------------------------------------------------------
  103. // TTransaction::GetDataRecordUpdatePointer
  104. //--------------------------------------------------------------------------------
  105. AnUpdate<TDataRecord> TTransaction::GetDataRecordUpdatePointer(AConst<TDataRecord> cursor)
  106. {
  107.     TTransactionAwareObject* newUP = this->GetUpdatePointer(cursor);
  108.     return newUP->DataRecord();
  109. } // TTransaction::GetDataRecordUpdatePointer
  110.  
  111. //--------------------------------------------------------------------------------
  112. // TTransaction::CommitChanges
  113. //--------------------------------------------------------------------------------
  114. void TTransaction::CommitChanges()
  115. {
  116.     //
  117.     // Give every update pointer one last chance to do non-failsafe
  118.     // operations before the transaction will actually commit.
  119.     //
  120.     for(TUpdatePointerCollectionIterator preIter(this); preIter.More(); preIter.Next())
  121.     {
  122.         preIter.Current()->UpdatePointer()->PrepareToCommit(this);
  123.     }
  124.     
  125.     //
  126.     // Once everyone is prepared, call CommitChanges.  Throwing
  127.     // an exception from CommitChanges is not allowed!
  128.     //
  129.     TUpdatePointerCollectionIterator iter(this);
  130.     while(iter.More())
  131.     {
  132.         //
  133.         // We don't provide a 'DeleteCurrent' method, because
  134.         // the TUpdatePointerCollectionIterator is not allowed
  135.         // to delete update pointers.  Only the transaction
  136.         // can do that.
  137.         //
  138.         TTransactionAwareObject* currentUP = iter.Current()->UpdatePointer();
  139.         currentUP->CommitChanges(this);
  140.         iter.Next();
  141.         
  142.         //
  143.         // Tell the object that it no longer belongs to a transaction;
  144.         //
  145.         currentUP->SetTransaction(nil);
  146.     }
  147.     
  148.     //
  149.     // Once we have committed all of the changes and deleted
  150.     // all of the update pointers, reset the transaction.  It
  151.     // may then be re-used for the next transaction or deleted.
  152.     //
  153.     this->ClearUpdatePointerCollection();
  154. } // TTransaction::CommitChanges
  155.  
  156. //--------------------------------------------------------------------------------
  157. // TTransaction::DiscardChanges
  158. //--------------------------------------------------------------------------------
  159. void TTransaction::DiscardChanges()
  160. {
  161.     TUpdatePointerCollectionIterator        iter(this);
  162.     
  163.     while(iter.More())
  164.     {
  165.         //
  166.         // We don't provide a 'DeleteCurrent' method, because
  167.         // the TUpdatePointerCollectionIterator is not allowed
  168.         // to delete update pointers.  Only the transaction
  169.         // can do that.
  170.         //
  171.         TTransactionAwareObject* currentUP = iter.Current()->UpdatePointer();
  172.         currentUP->DiscardChanges(this);
  173.         iter.Next();
  174.         
  175.         //
  176.         // Tell the object that it no longer belongs to a transaction;
  177.         //
  178.         currentUP->SetTransaction(nil);
  179.     }
  180.     
  181.     //
  182.     // Once we have discarded all of the changes and deleted
  183.     // all of the update pointers, reset the transaction.  It
  184.     // may then be re-used for the next transaction or deleted.
  185.     //
  186.     this->ClearUpdatePointerCollection();
  187. } // TTransaction::DiscardChanges
  188.  
  189.